﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Internal.Reactors;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YZQ_DotNetArx.CAD;

namespace YZQ_DotNetArx.Test
{
    public class 几何类和几何计算
    {
        //判断点是否重合给定容差
        [CommandMethod("Pt1isEqualToPt2")]
        public static void XX()
        {
            Point2d pt1 = new Point2d(0, 0);
            Point2d pt2 = new Point2d(0.00001, 0.00001);
            Tolerance tol = new Tolerance(1.0e-4, 1.0e-4);
            if (pt1.IsEqualTo(pt2, tol))
            {
                Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
                ed.WriteMessage("\n两点重合");
            }
        }
        //计算交点
        [CommandMethod("IntersectWith")]
        public static void IntersectWith()
        {
            //创建所要计算交点的集合类对象
            CircularArc2d geArc = new CircularArc2d(Point2d.Origin, 50, 0, 6, Vector2d.XAxis, false);
            Line2d geLine = new Line2d(Point2d.Origin, new Point2d(10, 10));
            //计算并输出交点
            Point2d[] intPoints = geArc.IntersectWith(geLine);
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\n直线与圆弧有{0}个交点.", intPoints.Length);
            for (int i = 0; i < intPoints.Length; i++)
            {
                ed.WriteMessage($"\n交点{i}，（{intPoints[i].X}，{intPoints[i].Y}）");
            }
        }
        //多边形作为
        [CommandMethod("TestPolygonSelect1")]
        public static void TestPolyonSelect1()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            //声明一个Point3d类列表对象，用户存储多段线的顶点
            Point3dList pts= new Point3dList();
            //提示用户选择多段线
            PromptEntityResult per = ed.GetEntity("\n请选择多段线");
            if (per.Status != PromptStatus.OK) return;
            using (Transaction trans=Env.Database.TransactionManager.StartTransaction())
            {
                //转换为Polyline对象
                Polyline pline = trans.GetObject(per.ObjectId,OpenMode.ForRead) as Polyline;
                if (pline!=null)
                {
                    //遍历所选多段线的顶点并添加到Point3d类列表
                    for (int i = 0; i < pline.NumberOfVertices; i++)
                    {
                        Point3d point = pline.GetPoint3dAt(i);
                        pts.Add(point);
                    }
                }
                //窗口选择，仅选择完全位于多边形区域中的对象
                PromptSelectionResult psr=ed.SelectWindowPolygon(pts);
                if (psr.Status==PromptStatus.OK)
                {
                    Application.ShowAlertDialog("选择集中的实体的数量："+psr.Value.Count.ToString());
                }
                trans.Commit(); 
            }
        }
        //
        [CommandMethod("TestSelectException1")]
        public static void TestSelectException1()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed=doc.Editor;
            Point3d pt1 = Point3d.Origin;
            Point3d pt2=new Point3d(100,100,0);
            //交叉窗口选择，选择由pt1和pt2组成的矩形窗口包围的或包围的或相交的对象
            PromptSelectionResult psr=ed.SelectCrossingWindow(pt1, pt2);
            if (psr.Status==PromptStatus.OK)
            {
                Application.ShowAlertDialog("选择集中实体的数量:"+psr.Value.Count.ToString());
            }
        }


        //计算两条曲线相交之后形成的边界线
        [CommandMethod("CurveBoolean")]
        public static void CurveBoolean()
        {
            //提示用户选择要计算距离的两条直线
            ObjectIdCollection polyIds=new ObjectIdCollection();
            
        }
    }
}
